package com.sskj.trafficcontrol.service;

import com.sskj.trafficcontrol.model.pojo.TcNoticeInfo;
import com.sskj.trafficcontrol.util.DateUtil;
import com.sskj.trafficcontrol.util.WxPushServiceQcl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author : zhao kuo
 * @date : 2019/2/4 18:00
 * @description :
 */
@Component
public class NoticeTaskService {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private WxPushServiceQcl wxPushServiceQcl;

    @Scheduled(cron = "0 * * * * *")
    public void scheduled(){
        List<TcNoticeInfo> infos = noticeService.selectByTime();
        if(CollectionUtils.isNotEmpty(infos)){
            for (int count = 0; count < infos.size(); count++) {
                TcNoticeInfo info = infos.get(count);

                /*if(info.getOpenId().equals("oscv15YMI0d5XDHKX8BwjI4pRKpE")){
                    wxPushServiceQcl.pushOneUser(info.getNoticeCity(),info.getOpenId(), info.getFormId(),info.getNoticeType(),info.getUserCarNumber());
                    noticeService.updateNoticeCount(info);
                }*/

                if (info.getNoticeTime()!=null && check(info)) {
                    wxPushServiceQcl.pushOneUser(info.getNoticeCity(),info.getOpenId(), info.getFormId(),info.getNoticeType(),info.getUserCarNumber());
                    noticeService.updateNoticeCount(info);
                }
            }
        }
    }

    private boolean check(TcNoticeInfo info) {
        if(info.getCount()==1 && info.getPushStatus()==0 && checkNoticeTime(info.getNoticeTime())){
            return true;
        }else{
            return false;
        }
    }

    private boolean checkNoticeTime(Date noticeTime) {

        Date now = new Date();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(now);
        rightNow.set(Calendar.SECOND, 0);
        Date nowTime = rightNow.getTime();  //当前时间下限
        rightNow.add(Calendar.MINUTE,1); //1分钟后的当前时间上限

        //1分钟后的事件
        Date now1Minute = rightNow.getTime();

        long noticeT = noticeTime.getTime()/1000; //提醒时间 2019-03-09 02:38:00
        long now1MinuteT = now1Minute.getTime()/1000; //一分钟后 2019-03-08 02:39:20
        long nowT = nowTime.getTime()/1000;  //当前时间 2019-03-08 02:38:20

/*        System.out.println("当前时间下限 "+DateUtil.getDateStr(nowTime)+" "+nowT);
        System.out.println("提醒时间 "+DateUtil.getDateStr(noticeTime)+" "+noticeT);
        System.out.println("1分钟后的当前时间上限 "+DateUtil.getDateStr(now1Minute)+" "+now1MinuteT);*/

        if( noticeT<=now1MinuteT && noticeT>=nowT ){
            return true;
        }else{
            return false;
        }

    }
}
